import 'package:share_fit/app/models/base_model.dart';
import 'package:share_fit/app/tools/date_tool.dart';

class SportDetailData extends BaseModel {
  /// 开始时间，格式：yyyyMMddHHmmss
  String? time;

  /// 速度，单位：分米/秒
  int? speed;

  /// 卡路里，单位：千卡
  int? calorie;

  /// 海拔，单位：分米(海拔支持负数)
  int? elevation;

  /// 划水频率，单位：次/分钟
  int? trashFrequency;

  /// 划水频率，单位：次/分钟
  int? swimEfficiency;

  /// 步频，单位：步/分钟
  int? stepFrequency;

  /// 心率，单位：次/分钟
  int? heartRate;

  /// 跳绳速度，单位：次/分钟
  int? jumpSpeed;

  /// 燃脂速率，单位：千卡/min
  int? fatBurnRate;

  /// 燃糖速率，单位：千卡/min
  int? sugarBurnRate;

  /// 配速，单位：秒/公里
  int? pace;

  SportDetailData({
    this.time,
    this.speed = 0,
    this.calorie = 0,
    this.elevation = 0,
    this.trashFrequency = 0,
    this.swimEfficiency = 0,
    this.stepFrequency = 0,
    this.heartRate = 0,
    this.jumpSpeed = 0,
    this.fatBurnRate = 0,
    this.sugarBurnRate = 0,
    this.pace = 0,
  });

  Map toJson() => {
    'sugarBurnRate': sugarBurnRate,
    'fatBurnRate': fatBurnRate,
    'jumpSpeed': jumpSpeed,
    'stepFrequency': stepFrequency,
    'heartRate': heartRate,
    'elevation': elevation,
    'calorie': calorie,
    'speed': speed,
    'pace': pace,
    'time': time,
  };

  SportDetailData.fromJson(Map json) {
    SportDetailData sdd = SportDetailData();
    sdd.from(json);
  }

  @override
  bool from(Map map) {
    try {
      pace = map['pace'] ?? 0;
      stepFrequency = map['cadence'] ?? 0;
      heartRate = map['heartRate'] ?? 0;
      elevation = map['totalAltitude'] ?? (map['altitude'] ?? 0);

      if (map['timeInfo'] > 0) {
        DateTime dt = DateTool.getDateTimeFrom(map['timeInfo']);
        time = DateTool.getyyyyMMddHHmmss(dt);
      }

      return true;
    } catch (e) {
      return false;
    }
  }

  @override
  List<Object?> get props => [
        time,
        speed,
        calorie,
        elevation,
        trashFrequency,
        swimEfficiency,
        stepFrequency,
        heartRate,
        jumpSpeed,
        fatBurnRate,
        sugarBurnRate,
        pace,
      ];

  @override
  bool? get stringify => true;
}
